import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Class to correct the data that is delivered. A method
 * to check if the data is empty. A methode to return a valid
 * temp incase its 20% higher or lower and a method to calculate a 
 * value if the value is empty.
 * @author marcovellinga
 *
 */
public class fillData {
	
	/**
	 * Method to check if the string is empty
	 * @param data
	 * @return
	 */
	public boolean checkEmpty(String data){
		if(data.length() == 0){
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Method to check wether the temp meets the requirements.
	 * If not it will correct the value.
	 * @param stn
	 * @param data
	 * @return
	 */
	public double returnValidTemp(String stn, String data){
		
		double doubleData = Double.parseDouble(data);
		if(doubleData < (0.8 * extrapolatie(stn, "temp")) ){
			return (0.8 * extrapolatie(stn, "temp"));
		} else if(doubleData > (1.2 * extrapolatie(stn, "temp"))){
			return (1.2 * extrapolatie(stn, "temp"));
		} else {
			return Double.parseDouble(data);
		}
	}
	
	/**
	 * Function to generate a estimated value of data
	 * which is missing from the XML file
	 * @param stn
	 * @param dataType
	 * @return
	 */
	public double extrapolatie(String stn, String dataType){
		
		// Setting up connection
		Connection conn = null;
		 String url = "jdbc:mysql://localhost:3306/";
		  String dbName = "unwdmi";
		  String driver = "com.mysql.jdbc.Driver";
		  String userName = "root"; 
		  String password = "";
		  
		  try {
			  
				  Class.forName(driver).newInstance();
				  conn = DriverManager.getConnection(url+dbName,userName,password);
				  
				// Create query
			    Statement stmt = conn.createStatement();
			    ResultSet rs = stmt.executeQuery("SELECT " + dataType + " FROM data WHERE stn = " 
			    + stn + " LIMIT 0, 29");
			    
		
				double addData = 0.0;
		    	double calcAVG = 0.0;
		    	int countData = 1;
		    	double avgData = 0.0;
		    	
		      // Loop the result of the query
			  while (rs.next()) {
				  	
			        String getData = rs.getString(dataType);
			        calcAVG = Double.parseDouble(getData);
			        
			        addData += calcAVG;
			         
			        countData++;   
			    }
			  
			  conn.close();
			  
			  // Create average value
			  if(countData == 1){ 
				  return avgData;
			  }else {
				  avgData = addData / countData;
				  return avgData;
			  }
				  
		  }
		  catch(SQLException e){
			  System.out.println(e);
		  } catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  return 0.0;

	}
}
